// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import ControlScript from "../PublicScript/ControlScript";
import GameData_red_blue_man, {dataName, emitType} from "./GameData_red_blue_man";
import Music, {musicName} from "../PublicScript/Music";
import Money from "./Money";
import SkinShop from "./SkinShop";
import CoinAnim from "./CoinAnim";
import PowerControl from "../PublicScript/PowerControl";
import FlyPower from "./FlyPower";
import HeroControl from "./HeroControl";

const {ccclass, property} = cc._decorator;

@ccclass
export default class Welcome extends cc.Component {

    @property({tooltip: "主界面上的主角", type: sp.Skeleton})
    heroList: sp.Skeleton[] = [];
    @property(cc.Node)
    point: cc.Node = null;
    @property({tooltip: "皮肤商店", type: cc.Node})
    skinShop: cc.Node = null;
    @property({tooltip: "礼物", type: cc.Node})
    giftNode: cc.Node = null;
    @property({tooltip: "测试", type: cc.Node})
    edit_Box: cc.Node = null;

    static isFirst = true;
    static tricky = false;

    test(){
       /* let str = "This is a test statement";
        //test() 方法是一个正则表达式方法。
        //test() 方法用于检测一个字符串是否匹配某个模式，如果字符串中含有匹配的文本，则返回 true，否则返回 false。
        console.log(/e/.test(str));
        console.log(str.search("is"));*/
    }

    onLoad () {
        cc.game.on(emitType.changeMenuSkin, this.changeSkin, this);

        this.init().then();
        // this.test();
    }

    start () {
        this.changeSkin();
    }

    changeSkin(){
        this.heroList[0].setSkin("red"+GameData_red_blue_man.usingSkinIndex);
        this.heroList[1].setSkin("blue"+GameData_red_blue_man.usingSkinIndex);
    }

    static isLoad = false;
    static invincibleEffect = null;
    async init(){
        Music.stopSpecialMusic({name: musicName.water});
        Music.stopSpecialMusic({name: musicName.Mechanical_sound});
        SkinShop.experienceSkinIndex = null;
        HeroControl.manSkinName = null;

        //屏蔽商店
        // this.skinShop.active = QQSDKManner.GetInstance().mistakeState;
        // this.giftNode.active = QQSDKManner.GetInstance().mistakeState;
        // this.turntable.active = QQSDKManner.GetInstance().mistakeState;

        //获取无敌特效
        cc.loader.loadRes("Anim/anger", sp.SkeletonData, (err,res)=>{
            Welcome.invincibleEffect = res;
        });

        //弹出签到
        let date = new Date();
        let year = date.getFullYear();
        let month = date.getMonth();
        let day = date.getDate();
        let singInData = {};
        let data = ControlScript.loadData(dataName.singIn);
        if (data != null && data != undefined && data != "") {
            singInData = JSON.parse(data);
            if(singInData["singInDate"][0] != year || singInData["singInDate"][1] != month || singInData["singInDate"][2] != day){
                ControlScript.instantiate("singin");
            }
        }else{
            singInData["singInDate"] = [];
            ControlScript.instantiate("singin");
        }
    }

    isGameAnim = false;
    onclick(e,d){
        if(this.isGameAnim){
            return
        }

        let index = parseInt(d);
        Music.GamePlayEffect(musicName.click);
        if(index == 1){
            PowerControl.reducePower(()=>{
                this.isGameAnim = true;
                let pos1 = this.node.getChildByName("power").children[0].children[0].convertToWorldSpaceAR(cc.v2(0,0));
                let pos2 = this.node.getChildByName("startGame").convertToWorldSpaceAR(cc.v2(0,0));
                ControlScript.instantiate("flyPower",(res)=>{
                    res.getComponent(FlyPower).init(pos1, pos2, ()=>{
                        cc.director.loadScene("GameScene");
                        this.isGameAnim = false;
                    })
                })
            })
        }
        if(index == 2){
            ControlScript.instantiate("setting");
        }
        if(index == 3){
            ControlScript.instantiate("skinShop");
        }
        if(index == 4){
            ControlScript.instantiate("turntable");
        }
        if(index == 5){     //获取金币
            GameData_red_blue_man.showVideo({callback:()=>{
                    ControlScript.instantiate("coinAnim", (res)=>{
                        res.y = 100;
                        res.getComponent(CoinAnim).createCoin();
                        res.getComponent(CoinAnim).setPoint(this.point, 1000, true);
                    });
                }, name: "视频获取金币"});
        }
        if(index == 6){     //签到
            ControlScript.instantiate("singin");
        }
        if(index == 7){     //礼物
            ControlScript.instantiate("gift");
        }
    }

    editBox(str){
        GameData_red_blue_man.highestLevel = parseInt(str.string)
    }

    // update (dt) {}
}
